package cn.iocoder.yudao.module.picturebook.service.translation;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.translation.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.translation.TranslationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.picturebook.dal.mysql.translation.TranslationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.picturebook.enums.ErrorCodeConstants.*;

/**
 * 绘本中英翻译 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TranslationServiceImpl implements TranslationService {

    @Resource
    private TranslationMapper translationMapper;

    @Override
    public Long createTranslation(TranslationSaveReqVO createReqVO) {
        // 插入
        TranslationDO translation = BeanUtils.toBean(createReqVO, TranslationDO.class);
        translationMapper.insert(translation);

        // 返回
        return Long.valueOf(translation.getId());
    }

    @Override
    public void updateTranslation(TranslationSaveReqVO updateReqVO) {
        // 校验存在
        validateTranslationExists(updateReqVO.getId());
        // 更新
        TranslationDO updateObj = BeanUtils.toBean(updateReqVO, TranslationDO.class);
        translationMapper.updateById(updateObj);
    }

    @Override
    public void deleteTranslation(Long id) {
        // 校验存在
        validateTranslationExists(id);
        // 删除
        translationMapper.deleteById(id);
    }

    @Override
    public void deleteTranslationListByIds(List<Long> ids) {
        // 删除
        translationMapper.deleteByIds(ids);
    }


    private void validateTranslationExists(Long id) {
        if (translationMapper.selectById(id) == null) {
            throw exception(TRANSLATION_NOT_EXISTS);
        }
    }

    @Override
    public TranslationDO getTranslation(Long id) {
        return translationMapper.selectById(id);
    }

    @Override
    public PageResult<TranslationDO> getTranslationPage(TranslationPageReqVO pageReqVO) {
        return translationMapper.selectPage(pageReqVO);
    }

}